﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Loop Parse - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Loop Parse statement retrieves substrings (fields) from a string, one at a time." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Loop Parse</h1>

<p>Retrieves substrings (fields) from a string, one at a time.</p>

<pre class="Syntax"><span class="func">Loop Parse</span> String <span class="optional">, Delimiters, OmitChars</span></pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>String</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The string to analyze.</p>
  </dd>

  <dt>Delimiters</dt>
  <dd>
      <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
      <p>If this parameter is blank or omitted, each character of the input string will be treated as a separate substring.</p>
      <p id="CSV">If this parameter is <code>"CSV"</code>, the string will be parsed in standard comma separated value format. Here is an example of a CSV line produced by MS Excel:</p>
<pre class="no-highlight">"first field",SecondField,"the word ""special"" is quoted literally",,"last field, has literal comma"</pre>
      <p>Otherwise, <em>Delimiters</em> contains one or more  characters (case sensitive), each of which is used to determine where the boundaries between substrings occur.</p>
      <p>Delimiter characters are not considered to be part of the substrings themselves. In addition, if there is nothing between a pair of delimiters within the input string, the corresponding substring will be empty.</p>
      <p>例如: <code>','</code> (a comma) would divide the string based on every occurrence of a comma. Similarly, <code>A_Tab A_Space</code> would start a new substring every time a space or tab is encountered in the input string.</p>
      <p>To use a string as a delimiter rather than a character, first use <a href="StrReplace.htm">StrReplace</a> to replace all occurrences of the string with a single character that is never used literally in the text, e.g. one of these special characters: &cent;&curren;&yen;&brvbar;&sect;&copy;&ordf;&laquo;&reg;&micro;&para;. Consider this example, which uses the string &lt;br&gt; as a delimiter:</p>
      <pre>NewHTML := StrReplace(HTMLString, "&lt;br&gt;", "&cent;")
Loop Parse, NewHTML, "&cent;" <em>; Parse the string based on the cent symbol.</em>
{
...
}</pre>
  </dd>

  <dt>OmitChars</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>An optional list of characters (case sensitive) to exclude from the beginning and end of each substring. 例如, if <em>OmitChars</em> is <code>A_Space A_Tab</code>, spaces and tabs will be removed from the beginning and end (but not the middle) of every retrieved substring.</p>
    <p>If <em>Delimiters</em> is blank, <em>OmitChars</em> indicates which characters should be excluded from consideration (the loop will not see them).</p>
  </dd>

</dl>

<h2 id="Remarks">备注</h2>
<p>A string parsing loop is useful when you want to operate on each field contained in a string, one at a time. Parsing loops use less memory than <a href="StrSplit.htm">StrSplit</a> (though either way the memory use is temporary) and in most cases they are easier to use.</p>
<p id="LoopField">The built-in variable <strong>A_LoopField</strong> exists within any parsing loop. It contains the contents of the current substring (field). If an inner parsing loop is enclosed by an outer parsing loop, the innermost loop's field will take precedence.</p>
<p>Although there is no built-in variable &quot;A_LoopDelimiter&quot;, the example at the very bottom of this page demonstrates how to detect which delimiter was encountered for each field.</p>
<p>There is no restriction on the size of the input string or its fields.</p>
<p>To arrange the fields in a different order prior to parsing, use the <a href="Sort.htm">Sort</a> function.</p>
<p>请参阅 <a href="Loop.htm">Loop</a> 了解关于<a href="Block.htm">区块</a>, <a href="Break.htm">Break</a>, <a href="Continue.htm">Continue</a> 和 A_Index 变量(其存在于各种类型的循环中) 的相关信息.</p>
<h2 id="Related">相关</h2>
<p><a href="StrSplit.htm">StrSplit</a>, <a href="LoopRead.htm">file-reading loop</a>, <a href="Loop.htm">Loop</a>, <a href="Break.htm">Break</a>, <a href="Continue.htm">Continue</a>, <a href="Block.htm">Blocks</a>, <a href="Sort.htm">Sort</a>, <a href="FileSetAttrib.htm">FileSetAttrib</a>, <a href="FileSetTime.htm">FileSetTime</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Colors := "red,green,blue"
Loop parse, Colors, ","
{
    MsgBox "Color number " A_Index " is " A_LoopField
}</pre>
</div>

<div class="ex" id="ExFileRead">
<p><a href="#ExFileRead">#2</a>: Read the lines inside a variable, one by one (similar to a <a href="LoopRead.htm">file-reading</a> loop). A file can be loaded into a variable via <a href="FileRead.htm">FileRead</a>:</p>
<pre>Loop parse, FileContents, "`n", "`r"  <em>; Specifying `n prior to `r allows both Windows and Unix files to be parsed.</em>
{
    Result := MsgBox("Line number " A_Index " is " A_LoopField ".`n`nContinue?",, "y/n")
}
until Result = "No"</pre>
</div>

<div class="ex" id="ExClipboard">
<p><a href="#ExClipboard">#3</a>: This is the same as the example above except that it's for the clipboard. It's useful whenever the clipboard contains files, such as those copied from an open Explorer window (the program automatically converts such files to their file names):</p>
<pre>Loop parse, clipboard, "`n", "`r"
{
    Result := MsgBox("File number " A_Index " is " A_LoopField ".`n`nContinue?",, "y/n")
}
until Result = "No"</pre>
</div>

<div class="ex" id="ExCSV">
<p><a href="#ExCSV">#4</a>: Parse a comma separated value (CSV) file:</p>
<pre>Loop read, "C:\Database Export.csv"
{
    LineNumber := A_Index
    Loop parse, A_LoopReadLine, "CSV"
    {
        Result := MsgBox("Field " LineNumber "-" A_Index " is:`n" A_LoopField "`n`nContinue?",, "y/n")
        if Result = "No"
            return
    }
}</pre>
</div>

<div class="ex" id="ExDelimiter">
<p><a href="#ExDelimiter">#5</a>: Determining which delimiter was encountered.</p>
<pre><em>; Initialize string to search.</em>
Colors := "red,green|blue;yellow|cyan,magenta"
<em>; Initialize counter to keep track of our position in the string.</em>
Position := 0

Loop Parse, Colors, ",|;"
{
    <em>; Calculate the position of the delimiter at the end of this field.</em>
    Position += StrLen(A_LoopField) + 1
    <em>; Retrieve the delimiter found by the parsing loop.</em>
    Delimiter := SubStr(Colors, Position, 1)

    MsgBox "Field: " A_LoopField "`nDelimiter: " Delimiter
}</pre>
</div>

</body>
</html>